package de.luh.slimpp

import com.google.inject.Inject
import de.luh.slimpp.parser.antlr.SlimPPParser
import de.luh.slimpp.services.SlimPPGrammarAccess
import java.io.StringReader
import org.eclipse.xtext.ParserRule

import static org.junit.Assert.*

class RuleParserHelper {

	@Inject SlimPPParser parser

	def parseRule(String code, Class<?> rule) {
		val grammar = parser.grammarAccess;
		val getterName = 'get' + rule.simpleName + 'Rule'
		val getterMethod = typeof(SlimPPGrammarAccess).getMethod(getterName)
		val ruleObject = getterMethod.invoke(grammar) as ParserRule
		val result = parser.parse(ruleObject, new StringReader(code))
		result.syntaxErrors.forEach[println('synax error: ' + syntaxErrorMessage)]
		assertFalse(result.hasSyntaxErrors)
		return result.rootASTElement
	}
}
